package com.base.app.service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
















import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.app.dao.ArticlePictureDao;
import com.base.app.entity.Article;
import com.base.app.entity.ArticlePicture;
import com.base.data.exception.DataException;
import com.mysteel.page.Page;

@Service("articleServicePicture")
public class ArticlePictureServiceImpl implements ArticlePictureService
{
	@Autowired
	private ArticlePictureDao articlePictureDao;
	@Autowired
	private ArticleService articleService;

	@Override
	public void saveArticlePicture(ArticlePicture articlePicture)
	{
		Assert.notNull (articlePicture);
		
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("name" ,articlePicture.getName ());
		params.put ("url" ,articlePicture.getUrl ());
		
		List<Long> list = articlePictureDao.query (params);
		
		if(ObjectUtils.notEmpty (list))
		{
			throw new DataException("图片上传失败");
		}
		
		articlePictureDao.save (articlePicture);
	}

	@Override
	public void deleteArticlePicture(long id,HttpServletRequest request)
	{
		ArticlePicture arp = articlePictureDao.get (id);
		
		if(ObjectUtils.notNull (arp))
		{
			long articleId = arp.getArticleId ();
			Article ar = articleService.getArticleById (articleId);
			
			if(ObjectUtils.notNull (ar))
			{
				String myappPath = request.getSession().getServletContext().getRealPath ("/");
				String url = myappPath+"/channel/article/"+ar.getChannelId ()+"/"+arp.getName ();			
				File f = new File(url);
				if(f.exists ())
				{
					f.delete ();
				}
			}	
		}
		 
		articlePictureDao.delete (id); 
	}

	@Override
	public ArticlePicture getArticlePictureById(long id)
	{	
		return articlePictureDao.get (id);
	}

	@Override
	public Page<ArticlePicture> queryArticlePicture(Map<String, Object> params, int currentPageNum, int maxPageRowCount)
	{
		
		Page<ArticlePicture> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = articlePictureDao.count(params);

		if (count > 0)
		{
			page = new Page<ArticlePicture>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> ids = articlePictureDao.query(params);

			if (ObjectUtils.notEmpty(ids))
			{
				List<ArticlePicture> arps = new ArrayList<ArticlePicture>(ids.size());

				for (long id : ids)
				{
					arps.add(getArticlePictureById  (id));
				}

				page.setDatas(arps);
			}
		}

		return page;
	}

	@Override
	public void updateArticlePicture(ArticlePicture articlePicture)
	{
		Assert.notNull(articlePicture);
		
		ArticlePicture arp = articlePictureDao.get (articlePicture.getId ());
		
		if(ObjectUtils.notNull (arp))
		{
			arp.setArticleId (articlePicture.getArticleId ());
			arp.setName(articlePicture.getName ());
			arp.setUrl (articlePicture.getUrl ());
			arp.setLevel(articlePicture.getLevel ());
			arp.setStatus (articlePicture.isStatus ());
			arp.setLastAccess (articlePicture.getLastAccess ());
			arp.setAdminId (articlePicture.getAdminId ());
			arp.setAdminName(articlePicture.getAdminName());
			
			articlePictureDao.update (arp);
		}
	}

	
}
